<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="110px">
      <el-form-item label="类型名称" prop="nickName">
        <el-input
          v-model="queryParams.name"
          placeholder="请输类型名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型id" prop="phone">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入类型id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="refreshData">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-circle-plus-outline"
          size="mini"
          @click="addTopLevelCategory"
        >新增一级分类</el-button>
      </el-col>
    </el-row>
    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="categories"
      row-key="id"
      :default-expand-all="isExpandAll"
    >
      <el-table-column prop="name" label="类型名称" width="260"></el-table-column>
      <el-table-column prop="id" label="类型id" width="200"></el-table-column>
      <el-table-column prop="parentId" label="父类id" width="200"></el-table-column>
      <el-table-column prop="type" label="级别" width="100"></el-table-column>
      <el-table-column prop="chargeStandard" label="收费标准" width="200"></el-table-column>
      <el-table-column label="操作" width="250">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit-outline"
            @click="handleEdit(scope.row)"
            v-hasPermi="['system:post:edit']"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:post:remove']"
          >删除</el-button>
          <el-button
            v-if="scope.row.type < 2"
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row, scope.row.id)"
            v-hasPermi="['system:post:add']"
          >新增子类</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      title="编辑/新增分类"
      :visible.sync="editFormVisible"
      width="50%"
    >
      <el-form :model="editForm" ref="editFormRef" :rules="rules">
        <el-form-item label="类型名称" prop="name">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
        <el-form-item label="父类id" prop="parentId">
          <el-input v-model="editForm.parentId" :disabled="editForm.isNew"></el-input>
        </el-form-item>
        <el-form-item label="级别" prop="type">
          <el-input v-model="editForm.type" readonly></el-input>
        </el-form-item>
        <el-form-item label="收费标准" prop="chargeStandard">
          <el-input v-model="editForm.chargeStandard"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveCategoryForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCategory, saveCategory } from '@/api/category/category';

export default {
  name: "CategoryList",

  data() {
    return {
      loading: true,
      refreshTable: true,
      isExpandAll: true,
      categories: [],
      editFormVisible: false,
      showSearch: true,
      // 查询参数
      queryParams: {
        id: undefined,
        name: undefined
      },
      editForm: {
        id: '',
        name: '',
        parentId: '',
        type: '',
        chargeStandard: '',
        isNew: false
      }
    };
  },
  created() {
    this.getCategory();
  },
  methods: {
    handleQuery(){
      this.getCategory();
    },
    getCategory() {
      this.loading = true;
      console.log(this.queryParams);
      listCategory(this.queryParams).then(response => {
        this.categories = response.data;
        console.log("==========")
        console.log(response.data)
        this.loading = false;
      });
    },
    refreshData() {
      this.getCategory();
      this.queryParams = {
        id: undefined,
        name: undefined

      };
    },
    handleDelete(row) {
      if (row.children && row.children.length > 0) {
        this.$message.warning("此分类下有子分类，无法删除！");
        return;
      }

      this.$confirm('确定要删除此项吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const params = { id: row.id, deleted: 1 };
        this.saveCategory(params, row);
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    saveCategory(params, row) {
      saveCategory(params).then(() => {
        if (row) {
          Object.assign(row, params);
        } else {
          this.categories.push(params);
        }
        this.$message.success("保存成功！");
        this.editFormVisible = false;
        this.refreshData();
      }).catch(error => {
        console.error(error);
        this.$message.error("保存失败！");
      });
    },
    handleEdit(row) {
      this.editFormVisible = true;
      this.editForm = {
        id: row.id,
        name: row.name,
        parentId: row.parentId,
        type: row.type,
        chargeStandard: row.chargeStandard,
        isNew: false
      };
    },
    handleAdd(parentRow, parentId) {
      this.editFormVisible = true;
      this.editForm = {
        id: '',
        name: '',
        parentId: parentId || '',
        type: parentRow.type ? String(Number(parentRow.type) + 1) : '',
        chargeStandard: '',
        isNew: true
      };
    },
    addTopLevelCategory() {
      this.editForm = {
        id: '',
        name: '',
        parentId: '0', // 顶级分类的parentId为0
        type: '0', // 假设顶级分类的type为'0'，根据实际情况调整
        chargeStandard: '',
        isNew: true
      };
      this.editFormVisible = true;
    },
    saveCategoryForm() {
      this.$refs.editFormRef.validate(valid => {
        if (valid) {
          this.editForm.type = this.editForm.type.toString();
          this.saveCategory(this.editForm);
        } else {
          return false;
        }
      });
    }
  }
};
</script>

<style scoped>
.app-container {
  display: flex;
  flex-direction: column;
}
</style>
